package top.infopub.mgr.sts.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.sts.dao.OutputValueDao;
import top.infopub.mgr.sts.domain.OutputValueDomain;
import top.infopub.mgr.sts.service.OutputValueService;
import top.infopub.model.sts.OutputValue;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class OutputValueServiceImpl extends GenericServiceImpl<OutputValue, String> implements
		OutputValueService {

	@Autowired
	private OutputValueDao outputValueDao;
	
	@Override
	public GenericDao<OutputValue, String> getDao() {
		return outputValueDao;
	}

    @Override
    public Page<OutputValueDomain> selectDataByDomainWhere(Page<OutputValueDomain> page, String type,
                                                         String orgId, String proName, String year) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Map<String, Object> where = new HashMap<String, Object>();
        where.put("proName", proName);
        if(StringUtils.isNotEmpty(year)){
            where.put("belongsYear", Integer.parseInt(year));
        }
        if(type.equals("2")){//区域公司
            where.put("hrOrgAreaId", orgId);
        }else if(type.equals("3")){//项目公司
            where.put("hrOrgProId", orgId);
        }
        List<OutputValueDomain> outputValueDomains = outputValueDao.selectDataByDomainWhere(where).getResult();
        for(OutputValueDomain outputValueDomain : outputValueDomains){
            String monthOutAmount = outputValueDomain.getMonthOutAmount();//返回格式：月份=金额，例如1=12314.12
            String monthInAmount = outputValueDomain.getMonthInAmount();
            String monthDiffAmount = outputValueDomain.getMonthDiffAmount();
            if(StringUtils.isNotEmpty(monthOutAmount)){
                String[] monthAmountArr = monthOutAmount.split(",");
                for(int i=0; i< monthAmountArr.length; i++){
                    String[] monthAmount = monthAmountArr[i].split("=");
                    int month = Integer.parseInt(monthAmount[0]);
                    BigDecimal amount = new BigDecimal(monthAmount[1]);
                    switch(month){
                        case 1:
                            outputValueDomain.setOutSureAmount1(amount);
                            break;
                        case 2:
                            outputValueDomain.setOutSureAmount2(amount);
                            break;
                        case 3:
                            outputValueDomain.setOutSureAmount3(amount);
                            break;
                        case 4:
                            outputValueDomain.setOutSureAmount4(amount);
                            break;
                        case 5:
                            outputValueDomain.setOutSureAmount5(amount);
                            break;
                        case 6:
                            outputValueDomain.setOutSureAmount6(amount);
                            break;
                        case 7:
                            outputValueDomain.setOutSureAmount7(amount);
                            break;
                        case 8:
                            outputValueDomain.setOutSureAmount8(amount);
                            break;
                        case 9:
                            outputValueDomain.setOutSureAmount9(amount);
                            break;
                        case 10:
                            outputValueDomain.setOutSureAmount10(amount);
                            break;
                        case 11:
                            outputValueDomain.setOutSureAmount11(amount);
                            break;
                        case 12:
                            outputValueDomain.setOutSureAmount12(amount);
                            break;
                    }
                }
            }
            if(StringUtils.isNotEmpty(monthInAmount)){
                String[] monthAmountArr = monthInAmount.split(",");
                for(int i=0; i< monthAmountArr.length; i++){
                    String[] monthAmount = monthAmountArr[i].split("=");
                    int month = Integer.parseInt(monthAmount[0]);
                    BigDecimal amount = new BigDecimal(monthAmount[1]);
                    switch(month){
                        case 1:
                            outputValueDomain.setInSureAmount1(amount);
                            break;
                        case 2:
                            outputValueDomain.setInSureAmount2(amount);
                            break;
                        case 3:
                            outputValueDomain.setInSureAmount3(amount);
                            break;
                        case 4:
                            outputValueDomain.setInSureAmount4(amount);
                            break;
                        case 5:
                            outputValueDomain.setInSureAmount5(amount);
                            break;
                        case 6:
                            outputValueDomain.setInSureAmount6(amount);
                            break;
                        case 7:
                            outputValueDomain.setInSureAmount7(amount);
                            break;
                        case 8:
                            outputValueDomain.setInSureAmount8(amount);
                            break;
                        case 9:
                            outputValueDomain.setInSureAmount9(amount);
                            break;
                        case 10:
                            outputValueDomain.setInSureAmount10(amount);
                            break;
                        case 11:
                            outputValueDomain.setInSureAmount11(amount);
                            break;
                        case 12:
                            outputValueDomain.setInSureAmount12(amount);
                            break;
                    }
                }
            }
            if(StringUtils.isNotEmpty(monthDiffAmount)){
                String[] monthAmountArr = monthDiffAmount.split(",");
                for(int i=0; i< monthAmountArr.length; i++){
                    String[] monthAmount = monthAmountArr[i].split("=");
                    int month = Integer.parseInt(monthAmount[0]);
                    BigDecimal amount = new BigDecimal(monthAmount[1]);
                    switch(month){
                        case 1:
                            outputValueDomain.setDiffAmount1(amount);
                            break;
                        case 2:
                            outputValueDomain.setDiffAmount2(amount);
                            break;
                        case 3:
                            outputValueDomain.setDiffAmount3(amount);
                            break;
                        case 4:
                            outputValueDomain.setDiffAmount4(amount);
                            break;
                        case 5:
                            outputValueDomain.setDiffAmount5(amount);
                            break;
                        case 6:
                            outputValueDomain.setDiffAmount6(amount);
                            break;
                        case 7:
                            outputValueDomain.setDiffAmount7(amount);
                            break;
                        case 8:
                            outputValueDomain.setDiffAmount8(amount);
                            break;
                        case 9:
                            outputValueDomain.setDiffAmount9(amount);
                            break;
                        case 10:
                            outputValueDomain.setDiffAmount10(amount);
                            break;
                        case 11:
                            outputValueDomain.setDiffAmount11(amount);
                            break;
                        case 12:
                            outputValueDomain.setDiffAmount12(amount);
                            break;
                    }
                }
            }
        }
        return (Page<OutputValueDomain>)outputValueDomains;
    }
}